package com.fare.taluo.web.action;

import java.io.IOException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.xalan.lib.sql.QueryParameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.plane.flight.ProductManageApplication;
import com.fare.taluo.util.JsonUtil;
import com.fare.taluo.vo.core.ProductEntityVO;

@Controller("productManageAction")
@Scope("prototype")
public class ProductManageAction extends BaseFdcAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1786974588445711797L;

	private static final Logger logger = LoggerFactory
			.getLogger(ProductManageAction.class);
	
	@Autowired
	private ProductManageApplication productManageApplication;
	private Map<String, Object> products = new HashMap<String, Object>();
	private List<ProductEntityVO> list;
	private String targetDate;
	private String travelDate;
	private ProductEntityVO product;
	private String ids;
	private String name;
	private String startCity;
	private String endCity;
	private String companyName;
	private String productType;

	public String toProductManage() throws Exception {
		returnPageURL = "/fare/productManage/productManage.jsp";
		return "dispatcher";
	}

	public void showProducts() throws IOException{
		list =  productManageApplication.findAll();
		products.put("Rows", list);
		products.put("Total", 10000);
		ServletActionContext.getResponse().getWriter().write(JsonUtil.toJson(products));
	}
	
	public void saveOrUpdate() throws ParseException{
		productManageApplication.saveOrUpdate(product);
	}
	
	public void findProductByConditions() throws IOException{
		String names="";
		String companyNames="";
		String startCitys="";
		String endCitys="";
		String productTypes="";
		names=URLDecoder.decode(name, "utf-8");
		companyNames=URLDecoder.decode(companyName,"UTF-8");
		startCitys=URLDecoder.decode(startCity, "UTF-8");
		endCitys=URLDecoder.decode(endCity, "UTF-8");
		productTypes=URLDecoder.decode(productType, "UTF-8");
		
		list = productManageApplication.findProductByConditions(names,companyNames,
				startCitys,endCitys,productTypes);
		products.put("Rows", list);
		products.put("Total", 10000);
		ServletActionContext.getResponse().getWriter().write(JsonUtil.toJson(products));
	}
	
	public void delete(){
		String[] id = ids.split(",");
		for(int i=0;i<id.length;i++){
			productManageApplication.delete(Integer.parseInt(id[i]));
		}
	}

	public ProductManageApplication getProductManageApplication() {
		return productManageApplication;
	}

	public void setProductManageApplication(
			ProductManageApplication productManageApplication) {
		this.productManageApplication = productManageApplication;
	}

	public List<ProductEntityVO> getList() {
		return list;
	}

	public void setList(List<ProductEntityVO> list) {
		this.list = list;
	}

	public ProductEntityVO getProduct() {
		return product;
	}

	public void setProduct(ProductEntityVO product) {
		this.product = product;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public Map<String, Object> getProducts() {
		return products;
	}

	public void setProducts(Map<String, Object> products) {
		this.products = products;
	}

	public String getStartCity() {
		return startCity;
	}

	public void setStartCity(String startCity) {
		this.startCity = startCity;
	}

	public String getEndCity() {
		return endCity;
	}

	public void setEndCity(String endCity) {
		this.endCity = endCity;
	}

	public String getProductType() {
		return productType;
	}

	public void setProductType(String productType) {
		this.productType = productType;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getTargetDate() {
		return targetDate;
	}

	public void setTargetDate(String targetDate) {
		this.targetDate = targetDate;
	}

	public String getTravelDate() {
		return travelDate;
	}

	public void setTravelDate(String travelDate) {
		this.travelDate = travelDate;
	}
}
